DZApp.service('roleResourceService', function(commonService) {
	return {
		showList: function() {
			var url = "/operationService/listSysRoleRes"; 
			return commonService.commonAjaxByPost(url);
		},
		showRoleSelect: function() {
			var url = "/operationService/listSysRole"; 
			return commonService.commonAjaxByPost(url);
		},
		showResourceSelect: function() {
			var url = "/operationService/listSysResource"; 
			return commonService.commonAjaxByPost(url);
		},
		editData: function(postData){
			var url = "/operationService/saveOrUpdateSysRoleRes";
			return commonService.commonAjaxByPost(url, postData);
		},
		delData: function(postData){
			var url = "/operationService/deleteSysRoleRes";
			return commonService.commonAjaxByPost(url, postData);
		}
	};
});

DZApp.controller('roleResourceController', function(
		$scope, roleResourceService, $state) {
	roleResourceService.showList().then(function(data) {
		$scope.showList = eval(data.bizData);
	});
	roleResourceService.showRoleSelect().then(function(data) {
		$scope.showRoleSelect = eval(data.bizData);
	});
	roleResourceService.showResourceSelect().then(function(data) {
		$scope.showResourceSelect = eval(data.bizData);
	});
	$scope.theObj = null;
	$scope.editData = function() {
		if (!bValidateObj.bValidateCall($('#editData'))) {
			return false;
		}
		var postData = $.param({
			sysRoleResStr : JSON.stringify($scope.edit),
			saveOrUpdate : ($scope.openTitle == '新增'?"C":"U")
		});
		roleResourceService.editData(postData).then(function(data) {
			if (data.retCode == 'success') {
				if ($scope.openTitle == '新增') {
					$state.reload();
				} else {
					$('#editData').modal('hide');
					for ( var p in $scope.theObj) {
						$scope.theObj[p] = $scope.edit[p];
					}
					$state.reload();
				}
			} else {
				bootboxWindow.alert('保存失败，' + data.errorMsg, null);
			}
		});
	};
	$scope.addInit = function() {
		$scope.openTitle = '新增';
		$scope.edit = {
			roleId : '',
			resourceId : '',
			isValid : 1
		};
	};
	$scope.setInit = function(_obj) {
		$scope.theObj = _obj;
		$scope.openTitle = '修改';
		$scope.edit = angular.copy(_obj);
	};
	$scope.delInit = function(_obj) {
		bootboxWindow.confirm('确定要删除该分配项？', function(){
			var postData = $.param({
				roleId : _obj.roleId,
				resourceId : _obj.resourceId
			});
			roleResourceService.delData(postData).then(function(data) {
				if (data.retCode == 'success') {
					$state.reload();
				} else {
					bootboxWindow.alert('删除失败，' + data.errorMsg, null);
				}
			});
		}, null);
	};
});